import django_filters
from .models import Goods
from django.db.models import Q


class GoodsFilter(django_filters.rest_framework.FilterSet):
    """
    价格区间以及销量和价格排行
    """
    pricemin = django_filters.NumberFilter(field_name="market_price", lookup_expr='gte',help_text='最低价格')     #过滤字段，market_price ，gte大于等于
    pricemax = django_filters.NumberFilter(field_name="market_price", lookup_expr='lte',help_text='最大价格')     #过滤字段，market_price，lte小雨等于
    top_category = django_filters.NumberFilter(method='top_category_filter',help_text='类目id')#

    def top_category_filter(self,queryset,name,value):  #自定义过滤函数
        # 查询category_id或者category__上乘分类的parent_category_id
        return queryset.filter(Q(category_id=value)|Q(category__parent_category_id=value)|Q(category__parent_category__parent_category_id=value))
    class Meta:
        model = Goods
        #过滤价格区间pricemin最低到pricemax最高 过滤是否热销
        fields  = ['pricemin', 'pricemax','is_hot']
